// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.unit.math;

import com.dustedpixels.jasmin.unit.Unit;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class ALU32 implements Unit {
  // (input) left input
  public int IN1;
  
  // (output) right input
  public int IN2;
  
  // (input) control line (4 bits)
  // bit 0: carry
  // bit 3: mode select
  // bits 1-2: operation
  public int CTRL;
  
  // (output)
  public int OUT;
  
  // (output) overflow bit
  public int OVERFLOW;
  
  // Internal units
  private final int bits;
  private final int mask;

  public ALU32(int bits) {
    this.bits = bits;
    this.mask = (1 << bits) - 1;
  }

  public void updateEndPoints() {
    if ((CTRL & 0x08) == 0) {
      // Logic mode
      if ((CTRL & 0x04) == 0x00) {
        if ((CTRL & 0x02) == 0) {
          OUT = IN1 & IN2;
        } else {
          OUT = IN1 | IN2;
        }
      } else {
        if ((CTRL & 0x02) == 0) {
          OUT = IN1 ^ IN2;          
        } else {
          OUT = ~(IN1 ^ IN2);
        }
      }
    } else {
      // Arithmetic mode
      int in1;
      int in2;
      
      if ((CTRL & 0x02) == 0) {
        in2 = 0;
      } else {
        in2 = IN2;
      }
      
      if ((CTRL & 0x04) == 0) {
        
      }
        
      OUT = IN1 + in2 + (CTRL & 0x01);
    }
  }
}
